package com.registration.service;

import com.registration.bean.OrderBean;
import com.registration.bean.ScheduleBean;
import com.registration.mapper.OrderMapper;
import com.registration.mapper.ScheduleMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class QueueServiceImpl implements QueueService {

    @Autowired
    private ScheduleMapper scheduleMapper;
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 获取可用排班信息
     * @param dept_id 科室ID
     * @param visit_date 预约日期
     * @return 可用排班信息
     */
    @Override
    public List<ScheduleBean> getAvailableSchedules(Integer dept_id, Date visit_date) {
        return scheduleMapper.getAvailableSchedules(dept_id, visit_date);
    }

    @Override
    public ScheduleBean getCurrentQueueInfo(Integer schedule_id) {
        ScheduleBean schedule = scheduleMapper.selectScheduleForUpdate(schedule_id);
        return schedule;
    }

    @Override
    public List<ScheduleBean> getDoctorCurrentSchedule(Integer doctor_id) {
        List<ScheduleBean> schedules = scheduleMapper.selectSchedulesByDoctorId(doctor_id);
        return schedules;
    }

    @Override
    @Transactional
    public boolean updateScheduleQuota(Integer schedule_id, int change) {
        ScheduleBean schedule = scheduleMapper.selectScheduleForUpdate(schedule_id);
        if (schedule == null) {
            return false;
        }
        int newQuota = schedule.getRemaining_quota() + change;
        if (newQuota < 0 || newQuota > schedule.getTotal_quota()) {
            return false;
        }

        // 更新状态，如果号源为0则标记为已满
        int newStatus = schedule.getStatus();
        if (newQuota == 0) {
            newStatus = 2; // 已满
        } else if (schedule.getStatus() == 2) {
            newStatus = 1; // 从已满变为可预约
        }

        scheduleMapper.updateRemainingQuota(schedule_id, newQuota);
        return true;
    }

    /**
     * 获取患者预约信息
     * @param patientId 患者ID
     * @return 患者预约信息
     */
    /**
 * 获取患者预约信息
 * @param patientId 患者ID
 * @return 患者预约信息
 */
    @Override
    public List<ScheduleBean> getQueueInfoByPatientId(Integer patientId) {
        // 首先通过OrderMapper获取该患者的所有已支付（未就诊）状态的订单
        List<OrderBean> orders = orderMapper.selectOrdersByPatientId(patientId, 1);

        // 创建一个列表存储排班信息
        List<ScheduleBean> schedules = new ArrayList<>();

        // 遍历订单，获取对应的排班信息
        for (OrderBean order : orders) {
            ScheduleBean schedule = scheduleMapper.selectScheduleForUpdate(order.getSchedule_id());
            if (schedule != null) {
                schedule.setVisit_no(order.getVisit_no());
                schedules.add(schedule);
            }
        }

        return schedules;
    }



    @Override
    public List<OrderBean> showQueue(Integer patientId) {
        return orderMapper.showQueue(patientId);
    }

    @Override
    public List<ScheduleBean> showQueueAndMy(Integer scheduleId) {
        return scheduleMapper.showQueueAndMy(scheduleId);
    }


}